<?php

namespace App\Http\Resources\Train\TrainOrder;

use App\Trip\Enums\TripTypes;
use App\Trip\Orders\TripOrderState;
use App\Trip\Orders\TripOrderStateDesc;
use App\Trip\Orders\TripType;
use App\Trip\Train\Seat\SeatName;
use App\Trip\Train\Seat\Seats;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use JsonSerializable;

class TrainOrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $order_info = parent::toArray($request);
        if (TripType::TRIP_TYPE_TRAIN != $order_info['trip_type']) {
            return $order_info;
        }
        if (isset($order_info['train_info'])) {
            $order_info['train_info'] = new TrainOrderTrainResource($order_info['train_info']);
        }

        if (isset($order_info['passengers'])) {
            $order_info['passengers'] = TrainOrderPassengerResource::collection($order_info['passengers']);
        }
        $is_snap = $order_info['snap_up'] == TripOrderState::ORDER_SNAP_UP;
        // 抢票订单 且 可替换的状态 也就是抢票状态 与订单状态有对应值时
        if ($is_snap && TripOrderState::canReplaceSnapState($order_info['order_state'])) {
            $order_info['order_state'] = $order_info['show_order_snap_state'] ?? $order_info['order_snap_state'];
        }
        $order_info['order_state_desc'] = TripOrderStateDesc::transDescByOrderState(TripTypes::TRAIN, $order_info['order_state']);
        $order_info['can_delete']       = in_array($order_info['order_state'], TripOrderState::can_delete_states()) ? 1 : 0;
        $order_info['is_valid']         = in_array($order_info['order_state'], TripOrderState::valid_states()) ? 1 : 0;
        $order_info['order_tip']        = TripOrderStateDesc::transIntroByOrderState(TripTypes::TRAIN, $order_info['order_state']);
        if (isset($order_info['max_pay_at']) && $order_info['order_state'] == TripOrderState::ORDER_STATE_CHANGE_WAITING_PAY) {
            $pay_date                = Carbon::parse($order_info['max_pay_at'])->toDateString();
            $pay_time                = Carbon::parse($order_info['max_pay_at'])->format('H:i');
            $order_info['order_tip'] = Str::replace(['{DATE}', '{TIME}'], [$pay_date, $pay_time], $order_info['order_tip']);
        }


        if (isset($order_info['other_fee'])) {
            $order_info['other_fee'] = OtherFeeResource::collection($order_info['other_fee']);
        }
        if ($is_snap) {
            unset(
                $order_info['alternative_train_codes'],
            );
        } else {
            unset(
                $order_info['alternative_seats'],
                $order_info['alternative_train_codes'],
                $order_info['alternative_train_dates']
            );
        }
        unset($order_info['id'],
            $order_info['user_id'],
            $order_info['online'],
            $order_info['is_show'],
            $order_info['trip_costs'],
            $order_info['out_order_id'],
            $order_info['updated_at'],
            $order_info['delete_state'],
            $order_info['deleted_at'],
            $order_info['origin_order_need_refund'],
            $order_info['price_details'],
            $order_info['order_service']);
        $order_info['selected_seats'] = collect(array_values(json_decode($order_info['selected_seats'], true) ?: []))
            ->map(function ($seat) {
                if ($seat['seat_type'] == 1) {
                    $prefix          = in_array($seat['seat'], Seats::SEAT_SIDES) ? "靠" : "";
                    $seat['seat_uy'] = SeatName::trans("{$prefix}{$seat['seat']}");
                } else {
                    $seat['seat_uy'] = SeatName::trans($seat['seat']);
                }
                return $seat;
            })
            ->toArray();
        return $order_info;
    }
}
